<?php
namespace app\admin\controller;
use think\Request;
use Firebase\JWT\JWT;
use think\Controller;

class Base extends Controller
{
    public function __construct()
    {
        header("Access-Control-Allow-Origin: *");//允许跨域
        header('Content-type: application/json');
        header("Access-Control-Allow-Methods:POST");
        header("access-control-allow-headers: Origin, X-Requested-With, Content-Type, Accept,WG-App-Version, WG-Device-Id, WG-Network-Type, WG-Vendor, WG-OS-Type, WG-OS-Version, WG-Device-Model, WG-CPU, WG-Sid, WG-App-Id, WG-Token,authorization");

        if (!request()->isPost()) {
            echo refuseVisit();die;
        }

        $this->checkToken();
    }

    public function checkToken()
    {
        $header = Request::instance()->header();

        if (empty($header['authorization']) || !isset($header['authorization'])){
            $info['code']    = '401';
            $info['message'] = 'Token不存在,拒绝访问';
            echo displayJson($info);die;
        }else{
            $this->verifyJwt($header['authorization']);
        }
    }

    //校验jwt权限API
    protected function verifyJwt($jwt)
    {
        $key = md5('taoerhuo');  //解密秘钥

        try {
            $jwtAuth  = json_encode(JWT::decode($jwt, $key, array('HS256')));
            $authInfo = json_decode($jwtAuth, true);

            if (empty($authInfo['us_id'])) {
                $info['code']    = '401';
                $info['message'] = '信息验证不通过,管理员不存在';
                echo displayJson($info);die;
            }

            $getUser = db('admin',[],false)->where('ad_id',$authInfo['us_id'])->find();

            if (!$getUser) {
                $info['code']    = '401';
                $info['message'] = '管理员状态异常';
                echo displayJson($info);die;
            }

        } catch (\Firebase\JWT\SignatureInvalidException $e) {

            $info['code']    = '401';
            $info['message'] = '会话信息无效';
            echo displayJson($info);die;

        } catch (\Firebase\JWT\ExpiredException $e) {

            $info['code']    = '401';
            $info['message'] = '会话已过期，请重新登录';
            echo displayJson($info);die;

        } catch (Exception $e) {
            $info['code']    = '401';
            $info['message'] = $e;
            echo displayJson($info);die;
        }
    }

    /*
     * 根据商品id数组获取商品分类
     */
    public function getClassifByGdIds($gdIds)
    {
        $newArr = array();

        if (!empty($gdIds)) {
            $res = db('classify',[],false)->where('cf_id','in',$gdIds)->select();

            $tmp = array();
            foreach ($res as $val) {
                $tmp[$val['cf_id']] = $val['cf_name'];
            }

            foreach ($gdIds as $key => $val) {
                $newArr[$key] = $tmp[$val];
            }
        }

        return $newArr;
    }

    /*
     * 根据分类id获取分类
     */
    public function getClassifyById($id)
    {
        $res = db('classify',[],false)->where('cf_id',$id)->find();

        if ($res) {
            return $res['cf_name'];
        } else {
            return false;
        }
    }


    /*
     * 根据id数组 获取用户邮箱 显示状态 以us_id为key
     * @param array $ids
     */
    public function getUserEmailByIds($ids)
    {
        $newArr = array();
        if (!empty($ids)) {
            $res = db('user',[],false)->where('us_id','in',$ids)->field('us_id,us_email,us_status')->select();

            foreach ($res as $val) {
                $newArr[$val['us_id']] = $val;
            }
        }

        return $newArr;
    }

    /*
     *根据用户id 查询用户邮箱 显示状态
     */
    public function getUserEmail($uid)
    {
        $row = db('user',[],false)->where('us_id',$uid)->find();

        if ($row) {
            $new = array();
            $new[$row['us_id']]['us_email']  = $row['us_email'];
            $new[$row['us_id']]['us_status'] =  $row['us_status'];

            return $new;
        } else {
            return false;
        }
    }

    /*
     * 根据商品id数组获取商品信息 以id为key
     */
    public function getGoodsByIds($ids)
    {
        $arr = array();

        if (!empty($ids)) {
            $res = db('goods',[],false)->where('gd_id','in',$ids)->field('gd_id,gd_title,gd_status')->select();

            foreach ($res as $val) {
                $arr[$val['gd_id']] = $val['gd_title'];
            }
        }

        return $arr;
    }


}
